package org.lex.net.proxy.ui;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.net.Proxy;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import org.lex.net.proxy.ProxyServer;


public class ProxyServerPane extends javax.swing.JPanel {
	private JLabel nameLabel;
	private JTextField nameTextField;
	private JPanel buttonsPane;
	private JPanel gluePanel;
	private JButton okButton;
	private JButton cancelButton;
	private JSeparator separator2;
	private JTextField portTextField;
	private JPasswordField passwordField;
	private JLabel passwordLabel;
	private JTextField userTextField;
	private JLabel userLabel;
	private JLabel portLabel;
	private JTextField hostTextField;
	private JLabel hostLabel;
	private JComboBox typeComboBox;
	private JLabel typeLabel;
	private JSeparator separator1;

	/**
	 * Auto-generated main method to display this JPanel inside a new JFrame.
	 */
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new ProxyServerPane());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}

	public ProxyServerPane() {
		super();
		initGUI();
	}

	public void setProxyServer(ProxyServer ps) {
		String name = "";
		Proxy.Type type = Proxy.Type.DIRECT;
		String host = "";
		String port = "";
		if (null != ps) {
			name = ps.getName();
			type = ps.getProxy().type();
			host = ps.getHost();
			port = Integer.toString(ps.getPort());
		}
		this.getNameTextField().setText(name);
		this.getTypeComboBox().setSelectedItem(type);
		this.getHostTextField().setText(host);
		this.getPortTextField().setText(port);
	}

	public ProxyServer getProxyServer() {
		String name = this.getNameTextField().getText();
		Proxy.Type type = Proxy.Type.valueOf(getTypeComboBox().getSelectedItem().toString().toUpperCase());
		String host = this.getHostTextField().getText().trim();
		String port = this.getPortTextField().getText().trim();
		if (false == this.checkString(name, host, port))
			return null;
		return new ProxyServer(name, type, host, Integer.parseInt(port));
	}

	public JButton getCancelButton() {
		if (cancelButton == null) {
			cancelButton = new JButton();
			cancelButton.setText("Cancel");
		}
		return cancelButton;
	}

	public JButton getOkButton() {
		if (okButton == null) {
			okButton = new JButton();
			okButton.setText("OK");
		}
		return okButton;
	}

	private boolean checkString(String... args) {
		for (String arg : args)
			if (null == arg || "".equals(arg))
				return false;
		return true;
	}

	private void initGUI() {
		try {
			GridBagLayout thisLayout = new GridBagLayout();
			this.setPreferredSize(new java.awt.Dimension(375, 225));
			this.setLayout(thisLayout);
			this.add(getNameLabel(), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(11,
					11, 0, 5), 0, 0));
			this.add(getNameTextField(), new GridBagConstraints(1, 0, 3, 1, 10.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
					new Insets(11, 0, 0, 12), 0, 0));
			this.add(getSeparator1(), new GridBagConstraints(0, 1, 4, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
					new Insets(11, 11, 0, 12), 0, 0));
			this.add(getTypeLabel(), new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(11,
					11, 0, 5), 0, 0));
			this.add(getTypeComboBox(), new GridBagConstraints(1, 2, 3, 1, 10.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
					new Insets(11, 0, 0, 12), 0, 0));
			this.add(getHostLabel(), new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(11,
					11, 0, 5), 0, 0));
			this.add(getHostTextField(), new GridBagConstraints(1, 3, 1, 1, 5.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
					new Insets(11, 0, 0, 5), 0, 0));
			this.add(getPortLabel(), new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(11, 0,
					0, 5), 0, 0));
			this.add(getPortTextField(), new GridBagConstraints(3, 3, 1, 1, 0.5, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
					new Insets(11, 0, 0, 12), 0, 0));
			this.add(getUserLabel(), new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(11,
					12, 0, 5), 0, 0));
			this.add(getUserTextField(), new GridBagConstraints(1, 4, 1, 1, 5.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
					new Insets(11, 0, 0, 5), 0, 0));
			this.add(getPasswordLabel(), new GridBagConstraints(2, 4, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(
					11, 0, 0, 5), 0, 0));
			this.add(getPasswordField(), new GridBagConstraints(3, 4, 1, 1, 0.5, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
					new Insets(11, 0, 0, 12), 0, 0));
			this.add(getSeparator2(), new GridBagConstraints(0, 5, 4, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
					new Insets(11, 11, 0, 12), 0, 0));
			this.add(getGluePanel(), new GridBagConstraints(0, 6, 4, 1, 10.0, 10.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,
					0, 0, 0), 0, 0));
			this.add(getButtonsPane(), new GridBagConstraints(0, 7, 4, 1, 10.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
					new Insets(11, 11, 12, 12), 0, 0));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private JLabel getNameLabel() {
		if (nameLabel == null) {
			nameLabel = new JLabel();
			nameLabel.setText("Name:");
		}
		return nameLabel;
	}

	private JTextField getNameTextField() {
		if (nameTextField == null) {
			nameTextField = new JTextField();
		}
		return nameTextField;
	}

	private JSeparator getSeparator1() {
		if (separator1 == null) {
			separator1 = new JSeparator();
		}
		return separator1;
	}

	private JLabel getTypeLabel() {
		if (typeLabel == null) {
			typeLabel = new JLabel();
			typeLabel.setText("Type:");
		}
		return typeLabel;
	}

	private JComboBox getTypeComboBox() {
		if (typeComboBox == null) {
			Object[] items = { Proxy.Type.HTTP, Proxy.Type.SOCKS };
			typeComboBox = new JComboBox(items);
		}
		return typeComboBox;
	}

	private JLabel getHostLabel() {
		if (hostLabel == null) {
			hostLabel = new JLabel();
			hostLabel.setText("Host:");
		}
		return hostLabel;
	}

	private JTextField getHostTextField() {
		if (hostTextField == null) {
			hostTextField = new JTextField();
		}
		return hostTextField;
	}

	private JLabel getPortLabel() {
		if (portLabel == null) {
			portLabel = new JLabel();
			portLabel.setText("Port:");
		}
		return portLabel;
	}

	private JTextField getPortTextField() {
		if (portTextField == null) {
			portTextField = new JTextField(6);
		}
		return portTextField;
	}

	private JLabel getUserLabel() {
		if (userLabel == null) {
			userLabel = new JLabel();
			userLabel.setText("User:");
		}
		return userLabel;
	}

	private JTextField getUserTextField() {
		if (userTextField == null) {
			userTextField = new JTextField();
		}
		return userTextField;
	}

	private JLabel getPasswordLabel() {
		if (passwordLabel == null) {
			passwordLabel = new JLabel();
			passwordLabel.setText("Password:");
		}
		return passwordLabel;
	}

	private JPasswordField getPasswordField() {
		if (passwordField == null) {
			passwordField = new JPasswordField(6);
		}
		return passwordField;
	}

	private JSeparator getSeparator2() {
		if (separator2 == null) {
			separator2 = new JSeparator();
		}
		return separator2;
	}

	private JPanel getGluePanel() {
		if (gluePanel == null) {
			gluePanel = new JPanel();
			FlowLayout gluePanelLayout = new FlowLayout();
			gluePanelLayout.setHgap(0);
			gluePanelLayout.setVgap(0);
			gluePanel.setLayout(gluePanelLayout);
		}
		return gluePanel;
	}

	private JPanel getButtonsPane() {
		if (buttonsPane == null) {
			buttonsPane = new JPanel();
			FlowLayout buttonsPaneLayout = new FlowLayout();
			buttonsPaneLayout.setAlignOnBaseline(true);
			buttonsPaneLayout.setAlignment(FlowLayout.TRAILING);
			buttonsPaneLayout.setHgap(0);
			buttonsPaneLayout.setVgap(0);
			buttonsPane.setLayout(buttonsPaneLayout);
			buttonsPane.add(getOkButton());
			buttonsPane.add(Box.createHorizontalStrut(11));
			buttonsPane.add(getCancelButton());
		}
		return buttonsPane;
	}
}
